import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr= new int [n];
        int[] gold = new int[n];
        for(int i = 0 ;i< n ;i++){
            arr[i] = sc.nextInt();
        }
        for(int i = 0 ;i< n;i++){
            int l = sc.nextInt();
            int r = sc.nextInt();
            if(l!=-1){
                gold[l-1] = gold[i]+1;
            }
            if(r!=-1){
                gold[r-1] = gold[i]-1;
            }
        }
        for(int i = 0;  i< n ; i++){
            if(gold[i] == 0){
                sum+=arr[i];
            }
        }
        System.out.println(sum);
    }
}
